package cc.owoo.godpen.network.throughput;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 接收流量
 * Created by nimensei
 * 2022-03-31 上午 01:58
 **/
public class ReadThroughput implements Throughput {
    private final AtomicLong size = new AtomicLong();// 流量

    /**
     * 创建并监听输入流
     *
     * @param input 输入流
     * @return 监听中的输入流
     */
    public InputStream count(InputStream input) {
        return new InputStream() {
            @Override
            public int read() throws IOException {
                int c = input.read();
                if (c != -1)
                    size.addAndGet(1);
                return c;
            }
        };
    }

    @Override
    public long size() {
        return size.get();
    }
}
